<template>
  <el-card>
    <div style="margin-bottom: 16px; display: flex; justify-content: space-between;">
      <el-input v-model="searchName" placeholder="按姓名搜索" style="width: 200px;" clearable @clear="fetchList" @keyup.enter="fetchList"/>
      <el-button type="primary" @click="openAddDialog">新增教师</el-button>
    </div>
    <el-table :data="teacherList" style="width: 100%">
      <el-table-column prop="id" label="ID" width="60"/>
      <el-table-column prop="name" label="姓名"/>
      <el-table-column prop="gender" label="性别"/>
      <el-table-column prop="title" label="职称"/>
      <el-table-column prop="department" label="院系"/>
      <el-table-column prop="email" label="邮箱"/>
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button size="small" @click="editTeacher(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增/编辑弹窗 -->
    <el-dialog v-model="dialogVisible" :title="form.id ? '编辑教师' : '新增教师'">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="form.gender" placeholder="请选择">
            <el-option label="男" value="男"/>
            <el-option label="女" value="女"/>
          </el-select>
        </el-form-item>
        <el-form-item label="职称" prop="title">
          <el-input v-model="form.title" />
        </el-form-item>
        <el-form-item label="院系" prop="department">
          <el-input v-model="form.department" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="saveTeacher">保存</el-button>
      </template>
    </el-dialog>
    <el-dialog v-model="roleDialogVisible" title="分配角色">
      <el-form :model="roleForm">
        <el-form-item label="角色">
          <el-select v-model="roleForm.role" placeholder="请选择角色">
            <el-option label="普通教师" value="teacher"/>
            <el-option label="系主任" value="director"/>
            <el-option label="管理员" value="admin"/>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="roleDialogVisible=false">取消</el-button>
        <el-button type="primary" @click="saveRole">保存</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { getTeacherList, addTeacher, updateTeacher, deleteTeacher, resetTeacherPassword, updateTeacherRole } from '@/api/teacher';

const teacherList = ref([]);
const searchName = ref('');
const dialogVisible = ref(false);
const form = ref({});
const formRef = ref(null);
const roleDialogVisible = ref(false);
const roleForm = ref({ id: null, role: '' });

const rules = {
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  gender: [{ required: true, message: '请选择性别', trigger: 'change' }],
  title: [{ required: true, message: '请输入职称', trigger: 'blur' }],
  department: [{ required: true, message: '请输入院系', trigger: 'blur' }],
  email: [{ type: 'email', message: '请输入正确的邮箱', trigger: 'blur' }]
};

function fetchList() {
  getTeacherList({ name: searchName.value }).then(res => {
    teacherList.value = res.data;
  });
}

function openAddDialog() {
  form.value = {};
  dialogVisible.value = true;
}

function editTeacher(row) {
  form.value = { ...row };
  dialogVisible.value = true;
}

function saveTeacher() {
  formRef.value.validate(valid => {
    if (!valid) return;
    const api = form.value.id ? updateTeacher : addTeacher;
    api(form.value).then(() => {
      dialogVisible.value = false;
      fetchList();
    });
  });
}

function handleDelete(id) {
  if (confirm('确定要删除该教师吗？')) {
    deleteTeacher(id).then(() => {
      fetchList();
    });
  }
}

function resetPassword(row) {
  if (confirm(`确定要重置【${row.name}】的密码吗？`)) {
    resetTeacherPassword(row.id).then(() => {
      alert('密码已重置为初始密码');
    });
  }
}

function openRoleDialog(row) {
  roleForm.value = { id: row.id, role: row.role || '' };
  roleDialogVisible.value = true;
}

function saveRole() {
  updateTeacherRole(roleForm.value.id, roleForm.value.role).then(() => {
    roleDialogVisible.value = false;
    fetchList();
  });
}

onMounted(fetchList);
</script> 